/* eslint no-var: 0, babel/object-shorthand: 0, vars-on-top: 0, func-names: 0 */
var webpack = require("webpack");
var path = require("path");
var isCI = process.env.CONTINUOUS_INTEGRATION === 'true';
var runCoverage = process.env.COVERAGE === 'true' || isCI;
var devBrowser = process.env.PHANTOM ? 'PhantomJS' : 'Chrome';

var preprocessors = ['webpack', 'sourcemap'];
var reporters = ['mocha'];

if (runCoverage) {
  webpackConfig = require('./webpack/test-coverage.config');
  reporters.push('coverage');

  if (isCI) {
    reporters.push('coveralls');
  }
}

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: [
      'mocha',
      'sinon-chai'
    ],

    files: [
      'test/index.js'
    ],

    preprocessors: {
      'test/index.js': preprocessors
    },
    webpack: {
        module: {
            noParse: [
                ///node_modules\/sinon\//
            ],
            loaders: [{
                test: /\.js|\.jsx$/,
                exclude: /node_modules/,
                loaders: ['babel-loader'],         
            }, {
                test: /\.js?$/,
                exclude: /node_modules/,
                loaders: ['babel-loader'],
            }, {
                test: /\.css$/,
                loader: 'style!css!autoprefixer?browsers=last 2 versions'
            }, {
                test: /\.png$/,
                loader: 'url?limit=8192&mimetype=image/png',
            }, {
                test: /\.less$/,
                loader: "style!css!less"
            }]
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    'NODE_ENV': JSON.stringify('development')
                }
            })
        ],
        devtool: 'eval',
    //devtool: 'inline-source-map',
        resolve: {
          root: path.resolve(__dirname, "./js"),
          // allow us to avoid including extension name
              extensions: ['', '.js', '.jsx'],
        },
      // required for enzyme to work properly
        externals: {
          'jsdom': 'window',
          'cheerio': 'window',
          'react/lib/ExecutionEnvironment': true,
          'react/lib/ReactContext': true
        },
    },

    webpackMiddleware: {
      noInfo: true
    },

    plugins: [
      "karma-mocha",
      "karma-sinon-chai",
      "karma-webpack",
      "karma-sourcemap-loader",
      'karma-coverage',   
      'karma-mocha-reporter',
      "karma-chrome-launcher",
    ],

    reporters: reporters,

    mochaReporter: {
      output: 'autowatch'
    },

    coverageReporter: {
      dir: '.coverage',
      reporters: [
        { type: 'html' },
        { type: 'lcovonly' }
      ]
    },

    port: 9876,

    colors: true,

    logLevel: config.LOG_INFO,

    autoWatch: true,

    browsers: [ isCI ? 'ChromeTravisCI' : devBrowser ],

    customLaunchers: {
      ChromeTravisCI: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },

    captureTimeout: 60000,
    browserNoActivityTimeout: 45000,

    singleRun: isCI
  });
};
